/**
 * Created by LI on 2020/6/16.
 */

public class Lyl {
    public static void main(String[] args) {
        Node node = create(20);
        print(node);
        Node newNode = remove(node, 2);
        print(newNode);
    }

    private static Node remove(Node node, int n) {
        Node first = node;
        Node second = node;
        for (int i = 0; i < n; i++) {
            if (first == null ) {
                System.out.println("参数值异常");
                return null;
            }
            first = first.next;
        }
        if (first == null) {
            return second.next;
        }
        while (first.next != null) {
            first = first.next;
            second = second.next;
        }
        System.out.println("删除节点为" + second.next.data);
        second.next = second.next.next;
        return node;
    }

    private static Node create(int nodeSize) {
        Node indexNode = null;
        while (nodeSize > 0) {
            Node node = new Node();
            node.data = (nodeSize - 1) + "*";
            node.next = indexNode;
            indexNode = node;
            nodeSize--;
        }
        return indexNode;
    }

    private static void print(Node node) {
        Node indexNode = node;
        while (indexNode != null) {
            System.out.print(indexNode.data);
            indexNode = indexNode.next;
        }
        System.out.println("##");
    }

    static class Node {
        String data;
        Node next;
    }
}
